<!DOCTYPE html>
<html lang="zn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入Vue -->
    <script src="../js/vue.js"></script>
    <title>Vue.set的使用</title>
</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="root">
        <h2>什么时间:{{when}}</h2>
        <h2>去了哪里:{{where}}</h2>
        <h2>去干嘛:{{what}}</h2>
        <h2>为什么去:{{why}}</h2>
        <hr>
        <h3>人员</h3>
        <button @click="addSex">添加一个性别属性</button>
        <ul>
            <li>姓名:{{person.name}}</li>
            <li>年龄:{{person.age}}</li>
            <li v-if="person.sex">性别:{{person.sex}}</li>
        </ul>
        <hr>
        <h4>目标</h4>
        <ul v-for="(personAim,index) in person.aim" :index="index">
            <li>{{personAim.name}}--{{personAim.value}}</li>
        </ul>
        <h4>爱好</h4>
        <ul>
            <li v-for="(h,index) in person.hobby" :index="index">
                {{h}}
            </li>
        </ul>
    </div>

    <script>
        Vue.config.productionTip = false


    // 创建一个实例
    const vm =  new Vue ({
        el: '#root',
        data() {
            return {
                // 对象
                when:Date.now(),
                where:'深圳',
                what:'打工',
                why:'活着',
                person:{
                    name:'伍镇浩',
                    age:25,
                    // 数组对象
                    aim:[
                        {
                            name:'挣大钱',
                            value:'100万'
                        },
                        {
                            name:'起大厝',
                            value:'70万'
                        },
                        {
                            name:'娶靓亩',
                            value:'30万'
                        }
                    ],
                    // 数组字符串
                    hobby:[
                        '抽烟','喝酒','洗脚'
                    ]
                }
            }
        },
        methods: {
            addSex(){
                // Vue.set(this.person,'sex','男')
                this.$set(this.person,'sex','男')
                this.$set(this.person.hobby,0,'吸烟')
            }
        },
    })
    </script>
</body>
</html>